package com.lucifer;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

/**
 * 功能描述:
 * <p>
 * Created by Mr.wang on 2017/6/6 13:55.
 */
public class modifyPDM {


    String pdmFile = "E:\\测试，好玩\\医养结合.pdm";
    String outputFile = "E:\\测试，好玩\\医养结合hhh.pdm";

    public static void main(String[] args) {
        new modifyPDM().AddComment();
    }


    /**
     * 为数据表和列添加Comment
     * 使用该方法时需要注意，保存文件后，Report中定义的格式会受影响
     */
    public void AddComment() {
        try{
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(pdmFile));
            System.out.println("document:"+document.toString());
            Element root = document.getRootElement();


            //找到每一个Column定义，添加或修改注释
            List<Node> nodes = document.selectNodes("/Model/o:RootObject/c:Children/o:Model/c:Packages/o:Package/c:Tables/o:Table/c:Columns/o:Column");
//            List<Node> nodes = document.selectNodes("/Model/o:RootObject/c:Children/o:Model/c:Packages/o:Package/c:Tables/o:Table");

            for (int i = 0; i < 3; i++) {
                System.out.println("nodeName:"+nodes.get(i).getName());
                System.out.println("nodeType:"+nodes.get(i).getNodeType());
                System.out.println("nodeTypeName:"+nodes.get(i).getNodeTypeName());
                System.out.println("text:"+nodes.get(i).getText());
                System.out.println("path:"+nodes.get(i).getPath());
                System.out.println("stringValue:"+nodes.get(i).getStringValue());
                System.out.println("document:"+nodes.get(i).getDocument());
                Node nameNode, commentNode,dataTypeNode;
                nameNode = nodes.get(i).selectSingleNode("a:Name");
                commentNode = nodes.get(i).selectSingleNode("a:Code");
                dataTypeNode = nodes.get(i).selectSingleNode("a:DataType");
                System.out.println("1111111:"+nameNode.getText().toString());
                System.out.println("222222:"+commentNode.getText().toString());
                System.out.println("333333:"+dataTypeNode.getText().toString());
            }

            /*Node nameNode, commentNode;
            Element element;
            String columnName, columnComment="";
            for (Node node : nodes) {
                System.out.println("node:"+node.toString());
                nameNode = node.selectSingleNode("a:Name");
                commentNode = node.selectSingleNode("a:Comment");
                columnName = nameNode.getText();
                //设置Column的注释。如果不存在注释，则取字段中文名+引号作为注释
                if (commentNode == null) {
                    element = nameNode.getParent();
                    element.addElement("a:Comment").setText(columnName + ":");
                } else {
                    columnComment = commentNode.getText();
                    if (!columnComment.startsWith(columnName + ":")) {
                        commentNode.setText(columnName + ":" + columnComment);
                    }
                }
            }

            //找到每一个Table定义，添加或修改注释
//            nodes = document.selectNodes("/Model/o:RootObject/c:Children/o:Model/c:Packages/o:Package/c:Tables/o:PdmTable");
            nodes = document.selectNodes("/Model/o:RootObject/c:Children/o:Model/c:Packages/o:Package/c:Tables/o:PdmTable");

            String tableName, tableComment;
            for (Node node : nodes) {
                nameNode = node.selectSingleNode("a:Name");
                commentNode = node.selectSingleNode("a:Comment");

                tableName = nameNode.getText();
                //设置Table的注释。如果不存在注释，则取字段中文名+引号作为注释
                if (commentNode == null) {
                    element = nameNode.getParent();
                    element.addElement("a:Comment").setText(tableName + ":");
                } else {
                    tableComment = commentNode.getText();
                    if (!columnComment.startsWith(tableName + ":")) {
                        commentNode.setText(tableName + ":" + tableComment);
                    }
                }
            }


            //保存为PDM文件
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");    // 指定XML编码
            XMLWriter writer = new XMLWriter(new FileWriter(outputFile),format);
            writer.write(document);
            writer.close();

            System.out.println("完成! 请用PowerDesigner打开文件：" + outputFile);
            System.out.println("Report中定义的格式会受影响，请重新定义输出的rft格式。");*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
